import path from 'node:path';
import URL from 'node:url';
import fs from 'node:fs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from 'rollup-plugin-typescript2';
import vue from '@vitejs/plugin-vue';
import postcss from 'rollup-plugin-postcss';

const __filename = URL.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const packages = ['utils', 'components'];

function getPackageRoots() {
  return packages.map((pkg) => path.resolve(__dirname, '../packages', pkg));
}

async function packageJson(root) {
  const jsonPath = path.resolve(root, 'package.json');
  const content = await fs.promises.readFile(jsonPath, 'utf-8');
  return JSON.parse(content);
}

/** 生成rollup配置 */
async function getRollupConfig(root) {
  const config = await packageJson(root);
  const tsconfig = path.resolve(root, 'tsconfig.json');
  const { name, formats } = config.buildOptions || {}; // 读取子包的打包配置
  const dist = path.resolve(root, 'dist');
  const entry = path.resolve(root, 'src/index.ts');
  const rollupOptions = {
    input: entry,
    sourceMap: true,
    external: ['vue'], // 不要把vue打包进去，工程里面已经包含vue了
    plugins: [
      nodeResolve(),
      commonjs(),
      typescript({
        tsconfig,
        compilerOptions: {
          outDir: dist,
        },
      }),
      vue({
        template: {
          compilerOptions: {
            nodeTransforms: [
              (node) => {
                if (node.type === 1 /* NodeType.Element */) {
                  node.props = node.props.filter((prop) => {
                    if (prop.type === 6 /* NodeTypes.Attribute */) {
                      return prop.name !== 'data-testid';
                    }
                    return true;
                  });
                }
              },
            ],
          },
        },
      }),
      postcss(),
    ],
    dir: dist,
  };
  const output = [];

  for (const format of formats) {
    const outputItem = {
      format,
      file: path.resolve(dist, `index.${format}.js`),
      sourcemap: true,
      globals: {
        vue: 'Vue',
      },
    };
    if (format === 'iife') {
      outputItem.name = name;
    }
    output.push(outputItem);
  }
  rollupOptions.output = output;
  return rollupOptions;
}

export async function getRollupConfigs() {
  const roots = getPackageRoots();
  const configs = await Promise.all(roots.map(getRollupConfig));
  const results = [];
  for (let i = 0; i < packages.length; i++) {
    results[packages[i]] = configs[i];
  }
  return results;
}

export default function clearDist(name) {
  const dist = path.resolve(__dirname, '../packages', name, 'dist');
  if (fs.existsSync(dist)) {
    fs.rmSync(dist, { recursive: true, force: true });
  }
}
